/*
 * DSP Example is part of the DSP Lecture at TEC-Costa Rica
 * Copyright (C) 2010  Pablo Alvarado
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file   mainwindow.cpp
 *         Implements the equalizer H(w) computation
 * \author Pablo Alvarado
 * \date   2010.12.12
 *
 * $Id: equalizer.cpp $
 */

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "jack.h"
#include "rom.h"

#include <string>

#undef _DSP_DEBUG
#define _DSP_DEBUG

#ifdef _DSP_DEBUG
#define _debug(x) std::cerr << x
#include <iostream>
#include <stdio.h>
#else
#define _debug(x)
#endif

/**
 * Precision used by trimming
 */
const float MainWindow::Epsilon = 0.001;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    verbose_(false),
    eqChanged_(true)
{
    int ret;
    ui->setupUi(this);

    /*volume slider */
    ui->volumeSlider->setValue(50);
    ui->volumeSlider->setRange(1,63);
    ui->volumeSlider->setSingleStep(5);
    ui->volumeSlider->setPageStep(15);
    ui->volumeSlider->setTickInterval(10);
    ui->volumeSlider->setTickPosition(QSlider::TicksBothSides);
    QTextStream(&vPos) << (int) ui->volumeSlider->value();
    QString cmdInitPCM = "amixer -q sset 'Master' "+ vPos;
    ret = system(cmdInitPCM.toLatin1().data());

    /* manage media buttons , They should be disabled */
    ui->playBut->setEnabled(false);
    ui->stopBut->setEnabled(false);


    /*
     * Set up a timer 4 times in a second to check if the user
     * changed the equalizer values, and if so, then create a new
     * filter response
     */
    timer_ = new QTimer(this);
    connect(timer_, SIGNAL(timeout()), this, SLOT(update()));

    timer_->start(250);

    dsp_ = new dspSystem;
    jack::init(dsp_);

    updateEqualizer();

    /* Initialize the FIR/IIR Spinbox values */
    ui->coefX1->setValue (iir_init_coefs[0]);
    ui->coefX2->setValue (iir_init_coefs[1]);
    ui->coefX3->setValue (iir_init_coefs[2]);
    ui->coefX4->setValue (iir_init_coefs[3]);
    ui->coefY0->setValue (iir_init_coefs[4]);
    ui->coefY1->setValue (iir_init_coefs[5]);
    ui->coefY2->setValue (iir_init_coefs[6]);

    ui->numCoefs->setValue (100);
    ui->firX0->setValue  (fir_init_coefs[0]);
    ui->firX1->setValue  (fir_init_coefs[1]);
    ui->firX2->setValue  (fir_init_coefs[2]);
    ui->firX3->setValue  (fir_init_coefs[3]);
    ui->firX4->setValue  (fir_init_coefs[4]);
    ui->firX5->setValue  (fir_init_coefs[5]);
    ui->firX6->setValue  (fir_init_coefs[6]);
    ui->firX7->setValue  (fir_init_coefs[7]);
    ui->firX8->setValue  (fir_init_coefs[8]);
    ui->firX9->setValue  (fir_init_coefs[9]);
    ui->firX10->setValue (fir_init_coefs[10]);
    ui->firX11->setValue (fir_init_coefs[11]);
    ui->firX12->setValue (fir_init_coefs[12]);
    ui->firX13->setValue (fir_init_coefs[13]);
    ui->firX14->setValue (fir_init_coefs[14]);

    /* parse some command line arguments */
    QStringList argv(QCoreApplication::arguments());
    QStringList::const_iterator it(argv.begin());
    while(it!=argv.end())
    {
        if ((*it)=="-v" || (*it)=="--verbose")
        {
            verbose_=true;
        }
        else if ((*it).indexOf(".wav",0,Qt::CaseInsensitive)>0)
        {
            ui->fileEdit->setText(*it);
            std::string tmp(qPrintable(*it));
            jack::playAlso(tmp.c_str());
        }
        ++it;
    }

}

MainWindow::~MainWindow()
{
    jack::close();
    delete timer_;
    delete ui;
    delete dsp_;
}

void MainWindow::update()
{
    if (eqChanged_)
    {
        _debug("Updating equalizer" << std::endl);
        dsp_->updateEqualizer();
        eqChanged_=false;
    }
}

/**
 * Convert a band value to the slider range
 */
int MainWindow::bandToSlider(const float band) const
{
    return static_cast<int>(band*50.0f + 0.5f);
}

/**
 * Convert a slider value to the band range
 */
float MainWindow::sliderToBand(const int val) const
{
    return float(val)/50.0;
}

/*
 * Reads the values on the real equalizer class an set the slider
 * values accordingly.
 */
void MainWindow::updateEqualizer()
{
    if (dsp_!=0)
    {
        equalizer& eq=*(dsp_->getEqualizer());
        int idx=0;
        ui->freq01Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq02Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq03Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq04Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq05Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq06Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq07Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq08Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq09Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq10Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq11Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq12Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq13Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq14Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq15Slider->setValue(bandToSlider(eq.getBand(idx++)));
        ui->freq16Slider->setValue(bandToSlider(eq.getBand(idx++)));
    }
}

void MainWindow::updateReverb()
{
    if (dsp_!=0)
    {
        reverb& rv = *(dsp_->getReverberator());

        ui->alphaSpinBox->setValue(rv.getAlpha());
        ui->delaySlider->setValue(static_cast<int>(rv.getDelay()+0.5f));
    }
}

void MainWindow::setFrequencySlider(
    const int idx,
    const int value)
{
    if (!eqChanged_)
    {
        eqChanged_=true;
        ui->presetsBox->setCurrentIndex(0);
    }
    float band = sliderToBand(value);
    dsp_->getEqualizer()->setBand(idx,band);
}

void MainWindow::on_freq01Slider_valueChanged(int value)
{
    setFrequencySlider(0,value);
}

void MainWindow::on_freq02Slider_valueChanged(int value)
{
    setFrequencySlider(1,value);
}

void MainWindow::on_freq03Slider_valueChanged(int value)
{
    setFrequencySlider(2,value);
}

void MainWindow::on_freq04Slider_valueChanged(int value)
{
    setFrequencySlider(3,value);
}

void MainWindow::on_freq05Slider_valueChanged(int value)
{
    setFrequencySlider(4,value);
}

void MainWindow::on_freq06Slider_valueChanged(int value)
{
    setFrequencySlider(5,value);
}

void MainWindow::on_freq07Slider_valueChanged(int value)
{
    setFrequencySlider(6,value);
}

void MainWindow::on_freq08Slider_valueChanged(int value)
{
    setFrequencySlider(7,value);
}

void MainWindow::on_freq09Slider_valueChanged(int value)
{
    setFrequencySlider(8,value);
}

void MainWindow::on_freq10Slider_valueChanged(int value)
{
    setFrequencySlider(9,value);
}

void MainWindow::on_freq11Slider_valueChanged(int value)
{
    setFrequencySlider(10,value);
}

void MainWindow::on_freq12Slider_valueChanged(int value)
{
    setFrequencySlider(11,value);
}

void MainWindow::on_freq13Slider_valueChanged(int value)
{
    setFrequencySlider(12,value);
}

void MainWindow::on_freq14Slider_valueChanged(int value)
{
    setFrequencySlider(13,value);
}

void MainWindow::on_freq15Slider_valueChanged(int value)
{
    setFrequencySlider(14,value);
}

void MainWindow::on_freq16Slider_valueChanged(int value)
{
    setFrequencySlider(15,value);
}

void MainWindow::on_alphaSpinBox_valueChanged(double value)
{
    ui->dialAlpha->setValue(static_cast<int>(value*1000.0+0.5));
    dsp_->getReverberator()->setAlpha(static_cast<float>(value));
}

void MainWindow::on_dialAlpha_dialMoved(int value)
{
    ui->alphaSpinBox->setValue(double(value)/1000.0);
}

void MainWindow::on_fileButton_clicked()
{
    selectedFiles_ =
        QFileDialog::getOpenFileNames(
                this,
                "Select one or more audio files to open",
                ui->fileEdit->text(),
                "WAV Files (*.wav)");

    if (!selectedFiles_.empty())
    {
        ui->fileEdit->setText(*selectedFiles_.begin());
        ui->stopBut->setEnabled(true);
        jack::stopFiles();
        QStringList::iterator it;
        for (it=selectedFiles_.begin(); it!=selectedFiles_.end(); ++it)
        {
            std::string tmp(qPrintable(*it));
            jack::playAlso(tmp.c_str());
        }
    }
}

void MainWindow::on_fileEdit_returnPressed()
{
    jack::stopFiles();
    std::string tmp(qPrintable(ui->fileEdit->text()));
    if (!tmp.empty())
    {
        jack::playAlso(tmp.c_str());
    }
}

void MainWindow::on_reverb_toggled(bool checked)
{
    _debug("Reverberator " << (checked?"activated":"deactivated") << std::endl);
    dsp_->setReverb(checked);
}

void MainWindow::on_equalizer_toggled(bool checked)
{
    _debug("Equalizer " << (checked?"activated":"deactivated") << std::endl);
    dsp_->setEqualizer(checked);
}

void MainWindow::on_filter60_toggled(bool checked)
{
    _debug("60 Hz filter " << (checked?"activated":"deactivated") << std::endl);
    dsp_->setFilter60(checked);
}

void MainWindow::on_iirfilter_toggled(bool checked)
{
    _debug("iirfilter " << (checked?"activated":"deactivated") << std::endl);
    dsp_->setIirfilter (checked);
}

void MainWindow::on_firfilter_toggled(bool checked)
{
    _debug("firfilter " << (checked?"activated":"deactivated") << std::endl);
    dsp_->setFirfilter (checked);
    if (!checked)
    {
        ui->numCoefs->show();
        ui->label_SetCoeffNum->show();
    }
    else
    {
        ui->numCoefs->hide();
        ui->label_SetCoeffNum->hide();
    }
}


void MainWindow::on_presetsBox_currentIndexChanged(int index)
{
    _debug("MainWindow::on_presetsBox_currentIndexChanged(" << index << ")\n");

    static const float presets[5][16] =
    {
        {
            0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
        },
        {
            1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
            1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f
        },
        {
            1.0f, 1.0f, 1.0f, 0.7f, 0.5f, 0.3f, 0.1f, 0.0f,
            0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
        },
        {
            0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
            0.0f, 0.1f, 0.3f, 0.5f, 0.7f, 1.0f, 1.0f, 1.0f
        },
        {
            0.0f, 0.0f, 0.1f, 0.3f, 0.5f, 0.7f, 1.0f, 1.0f,
            1.0f, 1.0f, 0.7f, 0.5f, 0.3f, 0.1f, 0.0f, 0.0f
        }
    };

    if ((index>0) || !eqChanged_)
    {
        for (int n=0; n<16; ++n)
        {
            dsp_->getEqualizer()->setBand(n,presets[index][n]);
        }
        eqChanged_=true;
        updateEqualizer();
    }

}

void MainWindow::on_resetButton_clicked()
{
    dsp_->getReverberator()->reset();
}

void MainWindow::on_delaySlider_valueChanged(int value)
{
    dsp_->getReverberator()->setDelay(float(value));
}

void MainWindow::on_exitBut_clicked()
{
    this->close();
}

void MainWindow::on_playBut_clicked()
{
    std::string tmp(qPrintable(ui->fileEdit->text()));
    if (ui->fileEdit->text() !="" )
    {
        if (!tmp.empty())
        {
            jack::playAlso(tmp.c_str());
        }
    }
    ui->playBut->setEnabled(false);
    ui->stopBut->setEnabled(true);

}

void MainWindow::on_stopBut_clicked()
{
    jack::stopFiles();
    ui->playBut->setEnabled(true);
    ui->stopBut->setEnabled(false);
}

void MainWindow::on_volumeSlider_valueChanged(int value)
{
    int ret;
    QString volumePos;
    QTextStream( &volumePos) << value;
    QString cmd2 = "amixer sset -q 'Master' " + volumePos;
    ret = system( cmd2.toLatin1().data());
    ui->volumeLCD->display((int)value*(100.0/63.0));
}

/*******************************************************************************
  USED FOR FIR AND IIR FILTERS
  *****************************************************************************/
void MainWindow::setCoeffIIRXN(
    int idx,
    float value)
{
    dsp_->getIirfilter()->coefficients.coefs_xn[idx] = value;
}

void MainWindow::setCoeffIIRYN(
    int idx,
    float value)
{
    dsp_->getIirfilter()->coefficients.coefs_yn[idx] = value;
}

void MainWindow::setCoeffFIRXN(
    int idx,
    float value)
{
    dsp_->getFirfilter ()->tFirMainStruct_.coefs[idx] = value;
    dsp_->getFirfilter ()->tFirMainStruct_.coefs[
            (dsp_->getFirfilter ()->tFirMainStruct_.coef_num -1) - idx ] =
            value;
}


void MainWindow::on_coefX1_valueChanged(double argx1)
{
    setCoeffIIRXN (0,static_cast<float>(argx1));
}

void MainWindow::on_coefX2_valueChanged(double argx2)
{
    setCoeffIIRXN (1,static_cast<float>(argx2));
}

void MainWindow::on_coefX3_valueChanged(double argx3)
{
    setCoeffIIRXN (2,static_cast<float>(argx3));
}


void MainWindow::on_coefY0_valueChanged(double argy0)
{
        setCoeffIIRYN (0,static_cast<float>(argy0));
}

void MainWindow::on_coefY1_valueChanged(double argy1)
{
    setCoeffIIRYN (1,static_cast<float>(argy1));
}

void MainWindow::on_coefY2_valueChanged(double argy2)
{
    setCoeffIIRYN (2,static_cast<float>(argy2));
}

/* ONLY FIR FILTER */

void MainWindow::on_firX0_valueChanged(double argfir0)
{
    setCoeffFIRXN(0,static_cast<float>(argfir0));

}

void MainWindow::on_firX1_valueChanged(double argfir1)
{
     setCoeffFIRXN(1,static_cast<float>(argfir1));
}

void MainWindow::on_firX2_valueChanged(double argfir2)
{
    setCoeffFIRXN(2,static_cast<float>(argfir2));
}

void MainWindow::on_firX3_valueChanged(double argfir3)
{
    setCoeffFIRXN(3,static_cast<float>(argfir3));
}

void MainWindow::on_firX4_valueChanged(double argfir4)
{
    setCoeffFIRXN(4,static_cast<float>(argfir4));
}

void MainWindow::on_firX5_valueChanged(double argfir5)
{
    setCoeffFIRXN(5,static_cast<float>(argfir5));
}

void MainWindow::on_firX6_valueChanged(double argfir6)
{
    setCoeffFIRXN(6,static_cast<float>(argfir6));
}

void MainWindow::on_firX7_valueChanged(double argfir7)
{
    setCoeffFIRXN(7,static_cast<float>(argfir7));
}

void MainWindow::on_firX8_valueChanged(double argfir8)
{
    setCoeffFIRXN(8,static_cast<float>(argfir8));
}

void MainWindow::on_firX9_valueChanged(double argfir9)
{
    setCoeffFIRXN(9,static_cast<float>(argfir9));
}

void MainWindow::on_firX10_valueChanged(double argfir10)
{
    setCoeffFIRXN(10,static_cast<float>(argfir10));
}

void MainWindow::on_firX11_valueChanged(double argfir11)
{
    setCoeffFIRXN(11,static_cast<float>(argfir11));
}

void MainWindow::on_firX12_valueChanged(double argfir12)
{
    setCoeffFIRXN(12,static_cast<float>(argfir12));
}

void MainWindow::on_firX13_valueChanged(double argfir13)
{
    setCoeffFIRXN(13,static_cast<float>(argfir13));
}

void MainWindow::on_firX14_valueChanged(double argfir14)
{
    setCoeffFIRXN(14,static_cast<float>(argfir14));
}

void MainWindow::on_resetIIRvalues_pressed()
{
    ui->coefX1->setValue (iir_init_coefs[0]);
    ui->coefX2->setValue (iir_init_coefs[1]);
    ui->coefX3->setValue (iir_init_coefs[2]);
    ui->coefX4->setValue (iir_init_coefs[3]);
    ui->coefY0->setValue (iir_init_coefs[4]);
    ui->coefY1->setValue (iir_init_coefs[5]);
    ui->coefY2->setValue (iir_init_coefs[6]);
    dsp_->getIirfilter ()->reset (dsp_->getIirfilter ()->coefficients);
}

void MainWindow::on_resetFIRvalues_pressed()
{
    ui->firX0->show ();
    ui->lx0->setText ("coefX[0]");
    ui->lx0->show ();

    ui->firX1->show ();
    ui->lx1->setText ("coefX[1]");
    ui->lx1->show ();

    ui->firX2->show ();
    ui->lx2->setText ("coefX[2]");
    ui->lx2->show ();

    ui->firX3->show ();
    ui->lx3->setText ("coefX[3]");
    ui->lx3->show ();

    ui->firX4->show ();
    ui->lx4->setText ("coefX[4]");
    ui->lx4->show ();

    ui->firX5->show ();
    ui->lx5->setText ("coefX[5]");
    ui->lx5->show ();

    ui->firX6->show ();
    ui->lx6->setText ("coefX[6]");
    ui->lx6->show ();

    ui->firX7->show ();
    ui->lx7->setText ("coefX[7]");
    ui->lx7->show ();

    ui->firX8->show ();
    ui->lx8->setText ("coefX[8]");
    ui->lx8->show ();

    ui->firX9->show ();
    ui->lx9->setText ("coefX[9]");
    ui->lx9->show ();

    ui->firX10->show ();
    ui->lx10->setText ("coefX[10]");
    ui->lx10->show ();

    ui->firX11->show ();
    ui->lx11->setText ("coefX[11]");
    ui->lx11->show ();

    ui->firX12->show ();
    ui->lx12->setText ("coefX[12]");
    ui->lx12->show ();

    ui->firX13->show ();
    ui->lx13->setText ("coefX[13]");
    ui->lx13->show ();

    ui->firX14->show ();
    ui->lx14->setText ("coefX[14]");
    ui->lx14->show ();

    dsp_->getFirfilter ()->tFirMainStruct_.coef_num = FIR_ORDER;
    ui->numCoefs->setValue (FIR_ORDER);

    ui->firX0->setValue  (fir_init_coefs[0]);
    ui->firX1->setValue  (fir_init_coefs[1]);
    ui->firX2->setValue  (fir_init_coefs[2]);
    ui->firX3->setValue  (fir_init_coefs[3]);
    ui->firX4->setValue  (fir_init_coefs[4]);
    ui->firX5->setValue  (fir_init_coefs[5]);
    ui->firX6->setValue  (fir_init_coefs[6]);
    ui->firX7->setValue  (fir_init_coefs[7]);
    ui->firX8->setValue  (fir_init_coefs[8]);
    ui->firX9->setValue  (fir_init_coefs[9]);
    ui->firX10->setValue (fir_init_coefs[10]);
    ui->firX11->setValue (fir_init_coefs[11]);
    ui->firX12->setValue (fir_init_coefs[12]);
    ui->firX13->setValue (fir_init_coefs[13]);
    ui->firX14->setValue (fir_init_coefs[14]);
    dsp_->getFirfilter ()->reset (&dsp_->getFirfilter ()->tFirMainStruct_);
}


void MainWindow::on_numCoefs_valueChanged(int arg1)
{
    dsp_->getFirfilter ()->tFirMainStruct_.coef_num = arg1;
   // printf("this is the new value %i\n",arg1);

    switch (arg1)
    {
    case 30:
        ui->firX0->show ();
        ui->lx0->setText ("coefX[0]");
        ui->lx0->show ();

        ui->firX1->show ();
        ui->lx1->setText ("coefX[1]");
        ui->lx1->show ();

        ui->firX2->show ();
        ui->lx2->setText ("coefX[2]");
        ui->lx2->show ();

        ui->firX3->show ();
        ui->lx3->setText ("coefX[3]");
        ui->lx3->show ();

        ui->firX4->show ();
        ui->lx4->setText ("coefX[4]");
        ui->lx4->show ();

        ui->firX5->show ();
        ui->lx5->setText ("coefX[5]");
        ui->lx5->show ();

        ui->firX6->show ();
        ui->lx6->setText ("coefX[6]");
        ui->lx6->show ();

        ui->firX7->show ();
        ui->lx7->setText ("coefX[7]");
        ui->lx7->show ();

        ui->firX8->show ();
        ui->lx8->setText ("coefX[8]");
        ui->lx8->show ();

        ui->firX9->show ();
        ui->lx9->setText ("coefX[9]");
        ui->lx9->show ();

        ui->firX10->show ();
        ui->lx10->setText ("coefX[10]");
        ui->lx10->show ();

        ui->firX11->show ();
        ui->lx11->setText ("coefX[11]");
        ui->lx11->show ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[12]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[13]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[14]");
        ui->lx14->show ();
        break;

    case 28:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->show ();
        ui->lx1->setText ("coefX[0]");
        ui->lx1->show ();

        ui->firX2->show ();
        ui->lx2->setText ("coefX[1]");
        ui->lx2->show ();

        ui->firX3->show ();
        ui->lx3->setText ("coefX[2]");
        ui->lx3->show ();

        ui->firX4->show ();
        ui->lx4->setText ("coefX[3]");
        ui->lx4->show ();

        ui->firX5->show ();
        ui->lx5->setText ("coefX[4]");
        ui->lx5->show ();

        ui->firX6->show ();
        ui->lx6->setText ("coefX[5]");
        ui->lx6->show ();

        ui->firX7->show ();
        ui->lx7->setText ("coefX[6]");
        ui->lx7->show ();

        ui->firX8->show ();
        ui->lx8->setText ("coefX[7]");
        ui->lx8->show ();

        ui->firX9->show ();
        ui->lx9->setText ("coefX[8]");
        ui->lx9->show ();

        ui->firX10->show ();
        ui->lx10->setText ("coefX[9]");
        ui->lx10->show ();

        ui->firX11->show ();
        ui->lx11->setText ("coefX[10]");
        ui->lx11->show ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[11]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[12]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[13]");
        ui->lx14->show ();

        break;

    case 26:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->show ();
        ui->lx2->setText ("coefX[0]");
        ui->lx2->show ();

        ui->firX3->show ();
        ui->lx3->setText ("coefX[1]");
        ui->lx3->show ();

        ui->firX4->show ();
        ui->lx4->setText ("coefX[2]");
        ui->lx4->show ();

        ui->firX5->show ();
        ui->lx5->setText ("coefX[3]");
        ui->lx5->show ();

        ui->firX6->show ();
        ui->lx6->setText ("coefX[4]");
        ui->lx6->show ();

        ui->firX7->show ();
        ui->lx7->setText ("coefX[5]");
        ui->lx7->show ();

        ui->firX8->show ();
        ui->lx8->setText ("coefX[6]");
        ui->lx8->show ();

        ui->firX9->show ();
        ui->lx9->setText ("coefX[7]");
        ui->lx9->show ();

        ui->firX10->show ();
        ui->lx10->setText ("coefX[8]");
        ui->lx10->show ();

        ui->firX11->show ();
        ui->lx11->setText ("coefX[9]");
        ui->lx11->show ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[10]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[11]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[12]");
        ui->lx14->show ();
        break;

    case 24:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->hide ();
        ui->lx2->hide ();

        ui->firX3->show ();
        ui->lx3->setText ("coefX[0]");
        ui->lx3->show ();

        ui->firX4->show ();
        ui->lx4->setText ("coefX[1]");
        ui->lx4->show ();

        ui->firX5->show ();
        ui->lx5->setText ("coefX[2]");
        ui->lx5->show ();

        ui->firX6->show ();
        ui->lx6->setText ("coefX[3]");
        ui->lx6->show ();

        ui->firX7->show ();
        ui->lx7->setText ("coefX[4]");
        ui->lx7->show ();

        ui->firX8->show ();
        ui->lx8->setText ("coefX[5]");
        ui->lx8->show ();

        ui->firX9->show ();
        ui->lx9->setText ("coefX[6]");
        ui->lx9->show ();

        ui->firX10->show ();
        ui->lx10->setText ("coefX[7]");
        ui->lx10->show ();

        ui->firX11->show ();
        ui->lx11->setText ("coefX[8]");
        ui->lx11->show ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[9]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[10]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[11]");
        ui->lx14->show ();
        break;

    case 22:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->hide ();
        ui->lx2->hide ();

        ui->firX3->hide ();
        ui->lx3->hide ();

        ui->firX4->show ();
        ui->lx4->setText ("coefX[0]");
        ui->lx4->show ();

        ui->firX5->show ();
        ui->lx5->setText ("coefX[1]");
        ui->lx5->show ();

        ui->firX6->show ();
        ui->lx6->setText ("coefX[2]");
        ui->lx6->show ();

        ui->firX7->show ();
        ui->lx7->setText ("coefX[3]");
        ui->lx7->show ();

        ui->firX8->show ();
        ui->lx8->setText ("coefX[4]");
        ui->lx8->show ();

        ui->firX9->show ();
        ui->lx9->setText ("coefX[5]");
        ui->lx9->show ();

        ui->firX10->show ();
        ui->lx10->setText ("coefX[6]");
        ui->lx10->show ();

        ui->firX11->show ();
        ui->lx11->setText ("coefX[7]");
        ui->lx11->show ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[8]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[9]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[10]");
        ui->lx14->show ();
        break;

    case 20:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->hide ();
        ui->lx2->hide ();

        ui->firX3->hide ();
        ui->lx3->hide ();

        ui->firX4->hide ();
        ui->lx4->hide ();

        ui->firX5->show ();
        ui->lx5->setText ("coefX[0]");
        ui->lx5->show ();

        ui->firX6->show ();
        ui->lx6->setText ("coefX[1]");
        ui->lx6->show ();

        ui->firX7->show ();
        ui->lx7->setText ("coefX[2]");
        ui->lx7->show ();

        ui->firX8->show ();
        ui->lx8->setText ("coefX[3]");
        ui->lx8->show ();

        ui->firX9->show ();
        ui->lx9->setText ("coefX[4]");
        ui->lx9->show ();

        ui->firX10->show ();
        ui->lx10->setText ("coefX[5]");
        ui->lx10->show ();

        ui->firX11->show ();
        ui->lx11->setText ("coefX[6]");
        ui->lx11->show ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[7]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[8]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[9]");
        ui->lx14->show ();
        break;


    case 18:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->hide ();
        ui->lx2->hide ();

        ui->firX3->hide ();
        ui->lx3->hide ();

        ui->firX4->hide ();
        ui->lx4->hide ();

        ui->firX5->hide ();
        ui->lx5->hide ();

        ui->firX6->show ();
        ui->lx6->setText ("coefX[0]");
        ui->lx6->show ();

        ui->firX7->show ();
        ui->lx7->setText ("coefX[1]");
        ui->lx7->show ();

        ui->firX8->show ();
        ui->lx8->setText ("coefX[2]");
        ui->lx8->show ();

        ui->firX9->show ();
        ui->lx9->setText ("coefX[3]");
        ui->lx9->show ();

        ui->firX10->show ();
        ui->lx10->setText ("coefX[4]");
        ui->lx10->show ();

        ui->firX11->show ();
        ui->lx11->setText ("coefX[5]");
        ui->lx11->show ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[6]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[7]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[8]");
        ui->lx14->show ();
        break;

    case 16:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->hide ();
        ui->lx2->hide ();

        ui->firX3->hide ();
        ui->lx3->hide ();

        ui->firX4->hide ();
        ui->lx4->hide ();

        ui->firX5->hide ();
        ui->lx5->hide ();

        ui->firX6->hide ();
        ui->lx6->hide ();

        ui->firX7->show ();
        ui->lx7->setText ("coefX[0]");
        ui->lx7->show ();

        ui->firX8->show ();
        ui->lx8->setText ("coefX[1]");
        ui->lx8->show ();

        ui->firX9->show ();
        ui->lx9->setText ("coefX[2]");
        ui->lx9->show ();

        ui->firX10->show ();
        ui->lx10->setText ("coefX[3]");
        ui->lx10->show ();

        ui->firX11->show ();
        ui->lx11->setText ("coefX[4]");
        ui->lx11->show ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[5]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[6]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[7]");
        ui->lx14->show ();
        break;

    case 14:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->hide ();
        ui->lx2->hide ();

        ui->firX3->hide ();
        ui->lx3->hide ();

        ui->firX4->hide ();
        ui->lx4->hide ();

        ui->firX5->hide ();
        ui->lx5->hide ();

        ui->firX6->hide ();
        ui->lx6->hide ();

        ui->firX7->hide ();
        ui->lx7->hide ();

        ui->firX8->show ();
        ui->lx8->setText ("coefX[0]");
        ui->lx8->show ();

        ui->firX9->show ();
        ui->lx9->setText ("coefX[1]");
        ui->lx9->show ();

        ui->firX10->show ();
        ui->lx10->setText ("coefX[2]");
        ui->lx10->show ();

        ui->firX11->show ();
        ui->lx11->setText ("coefX[3]");
        ui->lx11->show ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[6]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[7]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[8]");
        ui->lx14->show ();
        break;

    case 12:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->hide ();
        ui->lx2->hide ();

        ui->firX3->hide ();
        ui->lx3->hide ();

        ui->firX4->hide ();
        ui->lx4->hide ();

        ui->firX5->hide ();
        ui->lx5->hide ();

        ui->firX6->hide ();
        ui->lx6->hide ();

        ui->firX7->hide ();
        ui->lx7->hide ();

        ui->firX8->hide ();
        ui->lx8->hide ();

        ui->firX9->show ();
        ui->lx9->setText ("coefX[0]");
        ui->lx9->show ();

        ui->firX10->show ();
        ui->lx10->setText ("coefX[1]");
        ui->lx10->show ();

        ui->firX11->show ();
        ui->lx11->setText ("coefX[2]");
        ui->lx11->show ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[3]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[4]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[5]");
        ui->lx14->show ();
        break;

    case 10:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->hide ();
        ui->lx2->hide ();

        ui->firX3->hide ();
        ui->lx3->hide ();

        ui->firX4->hide ();
        ui->lx4->hide ();

        ui->firX5->hide ();
        ui->lx5->hide ();

        ui->firX6->hide ();
        ui->lx6->hide ();

        ui->firX7->hide ();
        ui->lx7->hide ();

        ui->firX8->hide ();
        ui->lx8->hide ();

        ui->firX9->hide ();
        ui->lx9->hide ();

        ui->firX10->show ();
        ui->lx10->setText ("coefX[0]");
        ui->lx10->show ();

        ui->firX11->show ();
        ui->lx11->setText ("coefX[1]");
        ui->lx11->show ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[2]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[3]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[4]");
        ui->lx14->show ();
        break;

    case 8:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->hide ();
        ui->lx2->hide ();

        ui->firX3->hide ();
        ui->lx3->hide ();

        ui->firX4->hide ();
        ui->lx4->hide ();

        ui->firX5->hide ();
        ui->lx5->hide ();

        ui->firX6->hide ();
        ui->lx6->hide ();

        ui->firX7->hide ();
        ui->lx7->hide ();

        ui->firX8->hide ();
        ui->lx8->hide ();

        ui->firX9->hide ();
        ui->lx9->hide ();

        ui->firX10->hide ();
        ui->lx10->hide ();

        ui->firX11->show ();
        ui->lx11->setText ("coefX[0]");
        ui->lx11->show ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[1]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[2]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[3]");
        ui->lx14->show ();
        break;

    case 6:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->hide ();
        ui->lx2->hide ();

        ui->firX3->hide ();
        ui->lx3->hide ();

        ui->firX4->hide ();
        ui->lx4->hide ();

        ui->firX5->hide ();
        ui->lx5->hide ();

        ui->firX6->hide ();
        ui->lx6->hide ();

        ui->firX7->hide ();
        ui->lx7->hide ();

        ui->firX8->hide ();
        ui->lx8->hide ();

        ui->firX9->hide ();
        ui->lx9->hide ();

        ui->firX10->hide ();
        ui->lx10->hide ();

        ui->firX11->hide ();
        ui->lx11->hide ();

        ui->firX12->show ();
        ui->lx12->setText ("coefX[0]");
        ui->lx12->show ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[1]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[2]");
        ui->lx14->show ();
        break;

    case 4:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->hide ();
        ui->lx2->hide ();

        ui->firX3->hide ();
        ui->lx3->hide ();

        ui->firX4->hide ();
        ui->lx4->hide ();

        ui->firX5->hide ();
        ui->lx5->hide ();

        ui->firX6->hide ();
        ui->lx6->hide ();

        ui->firX7->hide ();
        ui->lx7->hide ();

        ui->firX8->hide ();
        ui->lx8->hide ();

        ui->firX9->hide ();
        ui->lx9->hide ();

        ui->firX10->hide ();
        ui->lx10->hide ();

        ui->firX11->hide ();
        ui->lx11->hide ();

        ui->firX12->hide ();
        ui->lx12->hide ();

        ui->firX13->show ();
        ui->lx13->setText ("coefX[0]");
        ui->lx13->show ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[1]");
        ui->lx14->show ();
        break;

    case 2:
        ui->firX0->hide ();
        ui->lx0->hide ();

        ui->firX1->hide ();
        ui->lx1->hide ();

        ui->firX2->hide ();
        ui->lx2->hide ();

        ui->firX3->hide ();
        ui->lx3->hide ();

        ui->firX4->hide ();
        ui->lx4->hide ();

        ui->firX5->hide ();
        ui->lx5->hide ();

        ui->firX6->hide ();
        ui->lx6->hide ();

        ui->firX7->hide ();
        ui->lx7->hide ();

        ui->firX8->hide ();
        ui->lx8->hide ();

        ui->firX9->hide ();
        ui->lx9->hide ();

        ui->firX10->hide ();
        ui->lx10->hide ();

        ui->firX11->hide ();
        ui->lx11->hide ();

        ui->firX12->hide ();
        ui->lx12->hide ();

        ui->firX13->hide ();
        ui->lx13->hide ();

        ui->firX14->show ();
        ui->lx14->setText ("coefX[0]");
        ui->lx14->show ();
        break;
    }
}
